package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Repertory;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VRepertory;
import com.jxc.service.RepertoryServiceI;

@Service("repertoryService")
public class RepertoryServiceImpl implements RepertoryServiceI {

	@Autowired
	private BaseDaoI<Repertory> repertoryDao;

	@Override
	public List<VRepertory> list(VRepertory vrepertory) {
		String hql = "from Repertory t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vrepertory, hql, values);
		return this.changeModel(repertoryDao.find(hql, values));
	}

	private List<VRepertory> changeModel(List<Repertory> repertories) {
		List<VRepertory> vrepertories = new ArrayList<VRepertory>();
		if (repertories != null && repertories.size() > 0) {
			for (Repertory repertory : repertories) {
				VRepertory vrepertory = new VRepertory();
				BeanUtils.copyProperties(repertory, vrepertory);
				vrepertory.setUid(repertory.getUser().getId());
				vrepertory.setUname(repertory.getUser().getUsername());
				vrepertories.add(vrepertory);
			}
		}
		return vrepertories;
	}

	private List<Repertory> find(VRepertory verpertory) {
		String hql = "from Repertory t where isDelete=false ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(verpertory, hql, values);

		if (verpertory.getSort() != null && verpertory.getOrder() != null) {
			hql += " order by " + verpertory.getSort() + " " + verpertory.getOrder();
		}
		return repertoryDao.find(hql, values, verpertory.getPage(), verpertory.getRows());
	}

	private Long total(VRepertory vrepertory) {
		String hql = "select count(*) from Repertory t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vrepertory, hql, values);
		return repertoryDao.count(hql, values);
	}

	private String addWhere(VRepertory vrepertory, String hql, List<Object> values) {
		if (vrepertory.getId() != null) {
			hql += " and t.id = ? ";
			values.add(vrepertory.getId());
		}
		if (vrepertory.getUid() != null) {
			hql += " and t.user.id = ? ";
			values.add(vrepertory.getUid());
		}
		if (vrepertory.getName() != null && !vrepertory.getName().trim().equals("")) {
			hql += " and t.name like ? ";
			values.add("%%" + vrepertory.getName().trim() + "%%");
		}
		return hql;
	}

	@Override
	public DataGrid datagrid(VRepertory vrepertory) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vrepertory)));
		dg.setTotal(this.total(vrepertory));
		return dg;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					Repertory repertory = repertoryDao.get(Repertory.class, Long.parseLong(id.trim()));
					if (repertory != null) {
						repertory.setIsDelete(true);
						repertoryDao.update(repertory);
					}
				}
			}
		}

	}

	@Override
	public void editRepertoryInfo(VRepertory vrepertory) {
		// TODO Auto-generated method stub

	}

	@Override
	public void save(VRepertory vrepertory) {
		Repertory repertory = new Repertory();
		BeanUtils.copyProperties(vrepertory, repertory);
		if (vrepertory.getUid() != null) {
			repertory.setUser(new User(vrepertory.getUid()));
		}
		repertory.setIsDelete(false);
		this.repertoryDao.save(repertory);
	}

	@Override
	public void update(VRepertory vrepertory) {
		Repertory repertory = repertoryDao.get(Repertory.class, vrepertory.getId());
		BeanUtils.copyProperties(vrepertory, repertory, new String[] { "id" });
		if (vrepertory.getUid() != null) {
			repertory.setUser(new User(vrepertory.getUid()));
		}
		repertory.setIsDelete(false);
		this.repertoryDao.update(repertory);

	}

	@Override
	public VRepertory get(Long id) {
		return this.changeModel(repertoryDao.get(Repertory.class, id));
	}
	private VRepertory changeModel(Repertory repertory) {
		if (repertory == null)
			return null;

		VRepertory vRepertory = new VRepertory();
		BeanUtils.copyProperties(repertory, vRepertory);

		if (repertory.getUser() != null) {
			User user = repertory.getUser();
			vRepertory.setUid(user.getId());
			vRepertory.setUname(user.getUsername());
		}

		return vRepertory;
	}
}
